package br.com.news.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;

import br.com.news.utils.database.beans.EnqueteBean;
import br.com.news.utils.service.exception.ServiceException;

@Path("/enquetes")
public class EnqueteWs extends GenericWs<EnqueteBean>{

	private static final Logger logger = Logger.getLogger(EnqueteWs.class);
		
	@POST
	@Path("/insert")
	@Consumes(MediaType.APPLICATION_JSON)
	public void save(String json) {
		AlternativaEnqueteWs alternativa = new AlternativaEnqueteWs();
		try {
			EnqueteBean enquete = (EnqueteBean) convertFromJson(json, new EnqueteBean());
			Long id = RmiFactory.getEnqueteService().save(enquete);
			alternativa.save(id, (short) 1, "SIM");
			alternativa.save(id, (short) 2, "NAO");
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
		}		
	}
	
	@PUT
	@Path("/update")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Consumes(MediaType.APPLICATION_JSON)
	public Long update(String json) {		
		try {
			EnqueteBean enquete = (EnqueteBean) convertFromJson(json, new EnqueteBean());
			RmiFactory.getEnqueteService().update(enquete);
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
		}		
		return null;
	}
	
	@POST
	@Path("/findByQuestion")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Consumes(MediaType.APPLICATION_JSON)
	public String findByQuestion(String questao) {
		EnqueteBean enquete = (EnqueteBean) convertFromJson(questao, new EnqueteBean());
		return listToJson(RmiFactory.getEnqueteService().findByQuestion(enquete));
	}
	
	@GET
	@Path("/findPollByStatus/{status}")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public String findCategoriaByStatus(@PathParam(value="status") boolean status) {
		try {
			return listToJson(RmiFactory.getEnqueteService().findPollByStatus(status));
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			return "NOK";
		}
	}
	
	@GET
	@Path("/findPollActive")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public String pollActive(){
		try {
			return listToJson(RmiFactory.getEnqueteService().findPollActive());
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			return "NOK";
		}
	}
	
	@GET
	@Path("/last/poll")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public String lastPoll(){
		try {
			return listToJson(RmiFactory.getEnqueteService().lastPoll());
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);			
			return "NOK";
		}		
	}
	
	@GET
	@Path("/xml/last/poll")
	@Produces(MediaType.APPLICATION_XML)
	public List<EnqueteBean> lastPollXml(){
		try {
			return RmiFactory.getEnqueteService().lastPoll();
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);			
		}
		return null;
	}
	
	@GET
	@Path("/xml/findAll")
	@Produces(MediaType.APPLICATION_XML)
	public List<EnqueteBean> findAll() {
		try {
			return RmiFactory.getEnqueteService().findAll();
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);			
		}
		return null;
	}
}
